﻿using Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vision001.Models;

namespace Vision001.Helper
{
    /// <summary>
    /// 气缸运动
    /// </summary>
    public static class IoMotion
    {
        /// <summary>
        /// 气缸上升
        /// </summary>
        /// <returns></returns>

        public static bool AirUp(long timeout)
        {
            bool isok = false;
            //if (!Global._PqwIoCommon.GetDI((int)CommonIODefineDi.气缸到位))
            //{
            //    return false;
            //}
            //Global._PqwIoCommon.SetDO((int)CommonIODefineDo.电磁阀, true);
            Stopwatch sw = Stopwatch.StartNew();
            Task.Run(async () =>
            {
                while (true)
                {
                    //if (Global._PqwIoCommon.GetDI((int)CommonIODefineDi.气缸原点))
                    //{
                    //    isok = true;
                    //    break;
                    //}

                    if (sw.ElapsedMilliseconds > timeout)
                    {
                        break;
                    }

                    await Task.Delay(500);
                }
            }).Wait();
            return isok;
        }

        /// <summary>
        /// 气缸下降
        /// </summary>
        /// <returns></returns>
        public static bool AirDown(int timeout)
        {
            bool isok = false;
            //if (!Global._PqwIoCommon.GetDI((int)CommonIODefineDi.气缸原点))
            //{
            //    return false;
            //}
            //Global._PqwIoCommon.SetDO((int)CommonIODefineDo.电磁阀, false);
            Stopwatch sw = Stopwatch.StartNew();
            Task.Run(async () =>
            {
                while (true)
                {
                    //if (Global._PqwIoCommon.GetDI((int)CommonIODefineDi.气缸到位))
                    //{
                    //    isok = true;
                    //    break;
                    //}

                    if (sw.ElapsedMilliseconds > timeout)
                    {
                        break;
                    }

                    await Task.Delay(500);
                }
            }).Wait();
            return isok;
        }

        /// <summary>
        /// 设备报警
        /// </summary>
        public static void Error(int timeout)
        {
            Task.Run(async () =>
            {
                Global._PqwIoCommon.SetDO((int)CommonIODefineDo.红灯, true);
                await Task.Delay(timeout);
                Global._PqwIoCommon.SetDO((int)CommonIODefineDo.红灯, false);
            });
        }

        /// <summary>
        /// 电机启动
        /// </summary>
        public static void MotionRun()
        {
            try
            {
                Global._YPF_40R_2.Run();
                Global._YPF_40R_1.RunReverse();
            }
            catch (Exception e)
            {
                Mylog.Error(e);
            }
        }

        /// <summary>
        /// 电机停止
        /// </summary>
        public static void MotionStop()
        {
            try
            {
                Global._YPF_40R_1?.Stop();
                Global._YPF_40R_2?.Stop();
            }
            catch (Exception e)
            {
                Mylog.Error(e);
            }
        }

        /// <summary>
        /// 设置绿灯
        /// </summary>
        public static void OK()
        {
            Task.Run(async () =>
            {
                Global._PqwIoCommon.SetDO((int)CommonIODefineDo.绿灯, true);
            });
        }
    }
}